<script setup>
import { Lock, User } from '@element-plus/icons-vue'
import { ref, watch } from 'vue'
import { userRegisterService, userLoginService } from '@/api/user.js'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'
const isRemenber = ref(false)
const isLogin = ref(true)
const form = ref()
// 注册表单用户输入的内容
const register = ref({
  username: '',
  password: '',
  repassword: ''
})
// 注册表单检验规则
const registerRule = {
  username: [
    { required: true, message: '用户名不能为空', trigger: 'change' },
    { min: 6, max: 12, message: '请输入6~12位的用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    { pattern: /^\S{8,16}$/, message: '请输入8~16位的密码', trigger: 'change' }
  ],
  repassword: [
    {
      validator: (rule, value, callback) => {
        if (value !== register.value.password) return callback(new Error('两次输入密码不一样'))
        else callback()
      },
      trigger: 'blur'
    }
  ]
}
// 申请注册
const toRegister = async () => {
  // const form = ref()
  // await form.validate()
  // console.log(form.value)
  loading.value = true
  const result = await userRegisterService(register.value)
  ElMessage.success('注册成功')
  loading.value = false
  isLogin.value = true
  // console.log(result)
}
// 登录、注册切换时清空表单数据
watch(isLogin, (newValue, oldValue) => {
  register.value = {
    username: '',
    password: '',
    repassword: ''
  }
})
// 申请登录
const router = useRouter()
const login = async () => {
  // 表单预校验
  await form.value.validate()
  const result = await userLoginService(register.value)
  console.log(result)
  // 本地存入用户登录权证
  const store = useUserStore()
  store.getToken(result.token)
  ElMessage.success('登录成功')
  router.push('/')
}

</script>

<template>
  <el-row class=login>
    <!-- 左侧大背景 -->
    <el-col :span="12">
      <div class="bg"></div>
    </el-col>
    <!-- 登录表单 -->
    <el-col :span="6" :offset="3" class="form" v-if="isLogin">
      <el-form size="large" :model="register" :rules="registerRule" ref="form">
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input placeholder="请输入用户名" :prefix-icon="User" v-model="register.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" :prefix-icon="Lock" v-model="register.password"></el-input>
        </el-form-item>
        <el-form-item>
          <div class="flex1">
            <div>
              <el-checkbox v-model="isRemenber">记住我</el-checkbox>
            </div>
            <div>
              <a href="javascript:" style="text-decoration:none">忘记密码?</a>
            </div>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width:100%" @click="login">登录</el-button>
        </el-form-item>
        <el-form-item>
          <el-button text @click="isLogin=false">注册=></el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!-- 注册表单 -->
    <el-col :span="6" :offset="3" class="form" v-else>
      <el-form size="large" :model="register" :rules="registerRule">
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>
        <el-form-item prop="username">
          <el-input placeholder="请输入用户名" :prefix-icon="User" v-model="register.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input  placeholder="请输入密码" :prefix-icon="Lock" v-model="register.password"></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input placeholder="请再次输入密码" :prefix-icon="Lock" v-model="register.repassword"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="width:100%" type="primary" @click="toRegister">注册</el-button>
        </el-form-item>
        <el-form-item>
          <el-button text @click="isLogin=true"><=返回</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<style lang="scss" scoped>
.login {
  .bg {
    height: 100vh;
    border-radius:0 20px 20px 0;
    background:url('@/assets/logo2.png') no-repeat center,
    url('@/assets/login_bg.jpg') no-repeat center / cover;
  }
  .form {
    display:flex;
    flex-direction:column;
    justify-content:center;
    .flex1 {
      width:100%;
      display:flex;
      justify-content:space-between;
    }
    .flex2 {
      display:flex;
      align-items:center;
    }
  }
}

</style>